/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFTemplateGenerator.java
 *
 * Created on 30/10/2012, 08:32:07 PM
 */
package com.hidromedsa.measure.ui;

import com.hidromedsa.measure.controller.filesprocessor.FileDialog;
import com.hidromedsa.measure.controller.filesprocessor.FileProcessorService;
import com.hidromedsa.measure.controller.filesprocessor.FileRW;
import com.hidromedsa.measure.controller.templateprocessor.RulesStructure;
import com.hidromedsa.measure.controller.util.FileContants;
import com.hidromedsa.measure.entities.AbstractRule;
import com.hidromedsa.measure.entities.FieldMedicion;
import com.hidromedsa.measure.entities.RuleTypesEnum;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.bind.JAXBException;

/**
 *
 * @author Ronald Angel<angel.r@javeriana.edu.co/SIDRe,Takina - Pontificia
 * Universidad Javeriana>
 */
public class JFTemplateGenerator extends javax.swing.JFrame {

    JFTemplateGenerator frame;
    private final String columns[] = {"ID", "NAME"};
    TreeSet<FieldMedicion> fieldsOutput = new TreeSet<FieldMedicion>();
    List<FieldMedicion> fieldsInput = new ArrayList<FieldMedicion>();
    TreeSet<AbstractRule> rulesOutPut = new TreeSet<AbstractRule>();
    FileProcessorService fileProcessor;
    String frontLine = "";
    String nameTemplate = "";

    public FileContants getFileContants() {
        return fileContants;
    }

    public void setFileContants(FileContants fileContants) {
        this.fileContants = fileContants;
    }
    FileContants fileContants;

    /**
     * Creates new form JFTemplateGenerator
     */
    public JFTemplateGenerator() {
        initComponents();
        this.rbHeaders.setSelected(false);
        fnAddRadioButtons();
    }

    private void fnAddRadioButtons() {
        bgSeparatorColumn.add(rbSeparatorComma);
        bgSeparatorColumn.add(rbSeparatorVerticalBar);
        bgSeparatorColumn.add(rbSeparatorSemicolon);
        bgSeparatorColumn.add(rbSeparatorTab);
        bgSeparatorColumn.add(rbSeparatorOther);
        //
        bgValuesDefault.add(rbAddColumnNull);
        bgValuesDefault.add(rbAddColumnZero);
    }

    private void fnAddComboBox() {
        cbColumn1.setModel(new DefaultComboBoxModel());
        cbColumn2.setModel(new DefaultComboBoxModel());
        cbDuplicate.setModel(new DefaultComboBoxModel());
        cbPartition.setModel(new DefaultComboBoxModel());
        cbTemplate.setModel(new DefaultComboBoxModel());

        for (int i = 0; i < fieldsInput.size(); i++) {
            cbColumn1.addItem(String.valueOf(fieldsInput.get(i).getName()));
            cbColumn2.addItem(String.valueOf(fieldsInput.get(i).getName()));
            cbDuplicate.addItem(String.valueOf(fieldsInput.get(i).getName()));
            cbPartition.addItem(String.valueOf(fieldsInput.get(i).getName()));
        }

        List<File> listfile = fileProcessor.listTemplates(fileContants.getURL_TEMPLATES());
        for (int j = 0; j < listfile.size(); j++) {
            cbTemplate.addItem(String.valueOf(listfile.get(j).getName()));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgSeparatorColumn = new javax.swing.ButtonGroup();
        bgTypes = new javax.swing.ButtonGroup();
        bgValuesDefault = new javax.swing.ButtonGroup();
        jpManager = new javax.swing.JPanel();
        btUpload = new javax.swing.JButton();
        btSaveFileAs = new javax.swing.JButton();
        spImportColumn = new javax.swing.JScrollPane();
        tbImportColumn = new javax.swing.JTable();
        spExportColumns = new javax.swing.JScrollPane();
        tbExportColumn = new javax.swing.JTable();
        jpTemplate = new javax.swing.JPanel();
        cbTemplate = new javax.swing.JComboBox();
        btApplyTemplate = new javax.swing.JButton();
        bnDeleteTemplate = new javax.swing.JButton();
        tfNameTemplante = new javax.swing.JTextField();
        btSaveTemplate = new javax.swing.JButton();
        jpConcatenate = new javax.swing.JPanel();
        cbColumn1 = new javax.swing.JComboBox();
        tfStart1 = new javax.swing.JTextField();
        tfend1 = new javax.swing.JTextField();
        tfotherConcatenate = new javax.swing.JTextField();
        cbColumn2 = new javax.swing.JComboBox();
        tfStart2 = new javax.swing.JTextField();
        tfend2 = new javax.swing.JTextField();
        tfNameColumnConcatenate = new javax.swing.JTextField();
        btSaveAsConcatenate = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jpDuplicate = new javax.swing.JPanel();
        cbDuplicate = new javax.swing.JComboBox();
        btSaveAsDuplicate = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tfNameColumDuplicate = new javax.swing.JTextField();
        jpPartition = new javax.swing.JPanel();
        cbPartition = new javax.swing.JComboBox();
        tfPartCount = new javax.swing.JTextField();
        tfLength = new javax.swing.JTextField();
        tfNameColumnPartition = new javax.swing.JTextField();
        bnSaveAsPartition = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        tfSaveAs = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        tfFrontLine = new javax.swing.JTextField();
        bnSaveAsFrontline = new javax.swing.JButton();
        btAddRightColumn = new javax.swing.JButton();
        btAddleftColumn = new javax.swing.JButton();
        jpProperties = new javax.swing.JPanel();
        rbSeparatorComma = new javax.swing.JRadioButton();
        rbSeparatorSemicolon = new javax.swing.JRadioButton();
        rbSeparatorVerticalBar = new javax.swing.JRadioButton();
        rbSeparatorTab = new javax.swing.JRadioButton();
        rbSeparatorOther = new javax.swing.JRadioButton();
        tfOtherSeparator = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        rbHeaders = new javax.swing.JRadioButton();
        cbExtType = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        eltx = new javax.swing.JTextField();
        btUpRule = new javax.swing.JButton();
        bnDownRule = new javax.swing.JButton();
        btClear = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        tfNameAddColumn = new javax.swing.JTextField();
        rbAddColumnZero = new javax.swing.JRadioButton();
        rbAddColumnNull = new javax.swing.JRadioButton();
        btAddColumn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jpManager.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Template Manager", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
        jpManager.setName("Manager"); // NOI18N
        jpManager.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btUpload.setText("Upload");
        btUpload.setToolTipText("");
        btUpload.setName("btUpload"); // NOI18N
        btUpload.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btUploadMouseClicked(evt);
            }
        });
        btUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUploadActionPerformed(evt);
            }
        });
        jpManager.add(btUpload, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, -1, -1));

        btSaveFileAs.setText("Save");
        btSaveFileAs.setName("btSaveFileAs"); // NOI18N
        btSaveFileAs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btSaveFileAsMouseClicked(evt);
            }
        });
        btSaveFileAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveFileAsActionPerformed(evt);
            }
        });
        jpManager.add(btSaveFileAs, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 70, -1, -1));

        spImportColumn.setName("spImportColumn"); // NOI18N

        tbImportColumn.setAutoCreateRowSorter(true);
        tbImportColumn.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "NAME"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbImportColumn.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tbImportColumn.setName("tbImportColumn"); // NOI18N
        tbImportColumn.setRequestFocusEnabled(false);
        spImportColumn.setViewportView(tbImportColumn);
        tbImportColumn.getColumnModel().getColumn(0).setResizable(false);
        tbImportColumn.getColumnModel().getColumn(1).setResizable(false);

        jpManager.add(spImportColumn, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, 260, 160));

        spExportColumns.setName("spExportColumns"); // NOI18N

        tbExportColumn.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "NAME"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbExportColumn.setName("tbExportColumn"); // NOI18N
        tbExportColumn.setRequestFocusEnabled(false);
        spExportColumns.setViewportView(tbExportColumn);
        tbExportColumn.getColumnModel().getColumn(0).setResizable(false);
        tbExportColumn.getColumnModel().getColumn(1).setResizable(false);

        jpManager.add(spExportColumns, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 110, 240, 160));

        jpTemplate.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Template", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        jpTemplate.setName("jpTemplate"); // NOI18N
        jpTemplate.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cbTemplate.setName("cbTemplate"); // NOI18N
        jpTemplate.add(cbTemplate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 130, 30));

        btApplyTemplate.setText("Apply");
        btApplyTemplate.setName("btApplyTemplate"); // NOI18N
        btApplyTemplate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btApplyTemplateActionPerformed(evt);
            }
        });
        jpTemplate.add(btApplyTemplate, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 20, -1, -1));

        bnDeleteTemplate.setText("Delete");
        bnDeleteTemplate.setName("bnDeleteTemplate"); // NOI18N
        bnDeleteTemplate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnDeleteTemplateActionPerformed(evt);
            }
        });
        jpTemplate.add(bnDeleteTemplate, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 20, -1, -1));

        tfNameTemplante.setName("tfNameTemplante"); // NOI18N
        jpTemplate.add(tfNameTemplante, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, 130, -1));

        btSaveTemplate.setText("Save As");
        btSaveTemplate.setName("btSaveTemplate"); // NOI18N
        btSaveTemplate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btSaveTemplateMouseClicked(evt);
            }
        });
        btSaveTemplate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveTemplateActionPerformed(evt);
            }
        });
        jpTemplate.add(btSaveTemplate, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 20, -1, -1));

        jpManager.add(jpTemplate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 780, 60));

        jpConcatenate.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Concatenate", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        jpConcatenate.setName("jpConcatenate"); // NOI18N
        jpConcatenate.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cbColumn1.setName("cbColumn1"); // NOI18N
        jpConcatenate.add(cbColumn1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 120, 30));

        tfStart1.setName("tfStart1"); // NOI18N
        jpConcatenate.add(tfStart1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 30, 30));

        tfend1.setName("tfend1"); // NOI18N
        jpConcatenate.add(tfend1, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 40, 30, 30));

        tfotherConcatenate.setName("tfotherConcatenate"); // NOI18N
        jpConcatenate.add(tfotherConcatenate, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 40, 30, 30));

        cbColumn2.setName("cbColumn2"); // NOI18N
        jpConcatenate.add(cbColumn2, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 20, 130, 30));

        tfStart2.setName("tfStart2"); // NOI18N
        jpConcatenate.add(tfStart2, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 40, 30, 30));

        tfend2.setName("tfend2"); // NOI18N
        jpConcatenate.add(tfend2, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 40, 30, 30));

        tfNameColumnConcatenate.setName("tfNameColumnConcatenate"); // NOI18N
        jpConcatenate.add(tfNameColumnConcatenate, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 30, 120, -1));

        btSaveAsConcatenate.setText("Save As");
        btSaveAsConcatenate.setName("btSaveAsConcatenate"); // NOI18N
        btSaveAsConcatenate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btSaveAsConcatenateMouseClicked(evt);
            }
        });
        btSaveAsConcatenate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveAsConcatenateActionPerformed(evt);
            }
        });
        jpConcatenate.add(btSaveAsConcatenate, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 30, -1, -1));

        jLabel1.setText("+");
        jLabel1.setName("jLabel1"); // NOI18N
        jpConcatenate.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 40, -1, -1));

        jLabel2.setText("start");
        jLabel2.setName("jLabel2"); // NOI18N
        jpConcatenate.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 20, -1, -1));

        jLabel4.setText("+ space");
        jLabel4.setName("jLabel4"); // NOI18N
        jpConcatenate.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 20, -1, -1));

        jLabel5.setText("start:");
        jLabel5.setName("jLabel5"); // NOI18N
        jpConcatenate.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 20, -1, -1));

        jLabel6.setText("end:");
        jLabel6.setName("jLabel6"); // NOI18N
        jpConcatenate.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 20, -1, -1));

        jLabel7.setText("end");
        jLabel7.setName("jLabel7"); // NOI18N
        jpConcatenate.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 20, -1, -1));

        jLabel8.setText("=");
        jLabel8.setName("jLabel8"); // NOI18N
        jpConcatenate.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 40, -1, -1));

        jpManager.add(jpConcatenate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 340, 780, 80));

        jpDuplicate.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Duplicate", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        jpDuplicate.setName("jpDuplicate"); // NOI18N
        jpDuplicate.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cbDuplicate.setName("cbDuplicate"); // NOI18N
        jpDuplicate.add(cbDuplicate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 150, -1));

        btSaveAsDuplicate.setText("Save As");
        btSaveAsDuplicate.setName("btSaveAsDuplicate"); // NOI18N
        btSaveAsDuplicate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btSaveAsDuplicateMouseClicked(evt);
            }
        });
        btSaveAsDuplicate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveAsDuplicateActionPerformed(evt);
            }
        });
        jpDuplicate.add(btSaveAsDuplicate, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 30, -1, -1));

        jLabel3.setText("=");
        jLabel3.setName("jLabel3"); // NOI18N
        jpDuplicate.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 30, -1, -1));

        tfNameColumDuplicate.setName("tfNameColumDuplicate"); // NOI18N
        jpDuplicate.add(tfNameColumDuplicate, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 30, 140, -1));

        jpManager.add(jpDuplicate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 430, 780, 70));

        jpPartition.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Partition", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        jpPartition.setName("jpPartition"); // NOI18N
        jpPartition.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cbPartition.setName("cbPartition"); // NOI18N
        jpPartition.add(cbPartition, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 150, -1));

        tfPartCount.setName("tfPartCount"); // NOI18N
        jpPartition.add(tfPartCount, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 30, 50, -1));

        tfLength.setName("tfLength"); // NOI18N
        jpPartition.add(tfLength, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 30, 50, -1));

        tfNameColumnPartition.setName("tfNameColumnPartition"); // NOI18N
        jpPartition.add(tfNameColumnPartition, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 30, 120, -1));

        bnSaveAsPartition.setText("Save As");
        bnSaveAsPartition.setName("bnSaveAsPartition"); // NOI18N
        bnSaveAsPartition.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bnSaveAsPartitionMouseClicked(evt);
            }
        });
        bnSaveAsPartition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnSaveAsPartitionActionPerformed(evt);
            }
        });
        jpPartition.add(bnSaveAsPartition, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 30, -1, -1));

        jLabel9.setText("Start:");
        jLabel9.setName("jLabel9"); // NOI18N
        jpPartition.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 30, 50, 20));

        jLabel10.setText("End:");
        jLabel10.setName("jLabel10"); // NOI18N
        jpPartition.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 30, -1, -1));

        jLabel11.setText("=");
        jLabel11.setName("jLabel11"); // NOI18N
        jpPartition.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 30, -1, -1));

        jpManager.add(jpPartition, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 500, 780, 70));

        tfSaveAs.setName("tfSaveAs"); // NOI18N
        jpManager.add(tfSaveAs, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 70, 130, -1));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Frontline", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tfFrontLine.setName("tfFrontLine"); // NOI18N
        jPanel1.add(tfFrontLine, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 250, -1));

        bnSaveAsFrontline.setText("Save");
        bnSaveAsFrontline.setName("bnSaveAsFrontline"); // NOI18N
        bnSaveAsFrontline.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bnSaveAsFrontlineMouseClicked(evt);
            }
        });
        jPanel1.add(bnSaveAsFrontline, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, -1, -1));

        jpManager.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 570, 350, 60));

        btAddRightColumn.setText(">>");
        btAddRightColumn.setName("btAddRightColumn"); // NOI18N
        btAddRightColumn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddRightColumnActionPerformed(evt);
            }
        });
        jpManager.add(btAddRightColumn, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 120, 70, -1));

        btAddleftColumn.setText("Del");
        btAddleftColumn.setName("btAddleftColumn"); // NOI18N
        btAddleftColumn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddleftColumnActionPerformed(evt);
            }
        });
        jpManager.add(btAddleftColumn, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 150, 70, -1));

        jpProperties.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpProperties.setName("jpProperties"); // NOI18N
        jpProperties.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        rbSeparatorComma.setText("comma");
        rbSeparatorComma.setName("rbSeparatorComma"); // NOI18N
        rbSeparatorComma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSeparatorCommaActionPerformed(evt);
            }
        });
        jpProperties.add(rbSeparatorComma, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));
        rbSeparatorComma.getAccessibleContext().setAccessibleName("rbSeparatorColumn");

        rbSeparatorSemicolon.setText("semicolon");
        rbSeparatorSemicolon.setToolTipText("");
        rbSeparatorSemicolon.setName("rbSeparatorColumn"); // NOI18N
        rbSeparatorSemicolon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSeparatorSemicolonActionPerformed(evt);
            }
        });
        jpProperties.add(rbSeparatorSemicolon, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 10, -1, -1));

        rbSeparatorVerticalBar.setText("Vertical Bar");
        rbSeparatorVerticalBar.setName("rbSeparatorVerticalBar"); // NOI18N
        rbSeparatorVerticalBar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSeparatorVerticalBarActionPerformed(evt);
            }
        });
        jpProperties.add(rbSeparatorVerticalBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 10, -1, -1));

        rbSeparatorTab.setText("Tab");
        rbSeparatorTab.setName("rbSeparatorTab"); // NOI18N
        jpProperties.add(rbSeparatorTab, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 10, -1, -1));

        rbSeparatorOther.setText("other");
        rbSeparatorOther.setToolTipText("");
        rbSeparatorOther.setName("rbSeparatorColumn"); // NOI18N
        rbSeparatorOther.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSeparatorOtherActionPerformed(evt);
            }
        });
        jpProperties.add(rbSeparatorOther, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, -1, -1));
        rbSeparatorOther.getAccessibleContext().setAccessibleName("rbSeparatorColumn");

        tfOtherSeparator.setToolTipText("");
        tfOtherSeparator.setEnabled(false);
        tfOtherSeparator.setName("tfOtherSeparator"); // NOI18N
        tfOtherSeparator.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfOtherSeparatorActionPerformed(evt);
            }
        });
        jpProperties.add(tfOtherSeparator, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 10, 40, -1));

        jSeparator1.setName("jSeparator1"); // NOI18N
        jpProperties.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 540, 10));

        rbHeaders.setText("Headers");
        rbHeaders.setName("rbHeaders"); // NOI18N
        jpProperties.add(rbHeaders, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        cbExtType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Item", "txt", "csv", "dat", " " }));
        cbExtType.setName("cbExtType"); // NOI18N
        jpProperties.add(cbExtType, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 120, -1));

        jLabel12.setText("endLine");
        jLabel12.setName("jLabel12"); // NOI18N
        jpProperties.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 50, 60, 20));

        eltx.setName("eltx"); // NOI18N
        jpProperties.add(eltx, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 40, 40, 30));

        jpManager.add(jpProperties, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 20, 560, 80));

        btUpRule.setText("UP");
        btUpRule.setDoubleBuffered(true);
        btUpRule.setName("btUpRule"); // NOI18N
        btUpRule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUpRuleActionPerformed(evt);
            }
        });
        jpManager.add(btUpRule, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 190, 70, -1));

        bnDownRule.setText("DOWN");
        bnDownRule.setName("bnDownRule"); // NOI18N
        bnDownRule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnDownRuleActionPerformed(evt);
            }
        });
        jpManager.add(bnDownRule, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 220, 70, -1));

        btClear.setText("Nuevo");
        btClear.setName("btClear"); // NOI18N
        btClear.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btClearMouseClicked(evt);
            }
        });
        btClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btClearActionPerformed(evt);
            }
        });
        jpManager.add(btClear, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 70, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Values", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tfNameAddColumn.setName("tfNameAddColumn"); // NOI18N
        jPanel2.add(tfNameAddColumn, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 22, 130, -1));

        rbAddColumnZero.setText("Zero fill");
        rbAddColumnZero.setName("rbAddColumnZero"); // NOI18N
        jPanel2.add(rbAddColumnZero, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 55, -1, -1));

        rbAddColumnNull.setText("Null");
        rbAddColumnNull.setName("rbAddColumnNull"); // NOI18N
        jPanel2.add(rbAddColumnNull, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 84, -1, -1));

        btAddColumn.setText("Add Column");
        btAddColumn.setName("btAddColumn"); // NOI18N
        btAddColumn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddColumnActionPerformed(evt);
            }
        });
        jPanel2.add(btAddColumn, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, -1, -1));

        jpManager.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 110, 160, 170));

        getContentPane().add(jpManager, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 800, 640));
        jpManager.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfOtherSeparatorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfOtherSeparatorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfOtherSeparatorActionPerformed

    private void rbSeparatorOtherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSeparatorOtherActionPerformed
        // TODO add your handling code here:
        tfOtherSeparator.setEnabled(true);
        tfOtherSeparator.setText("");
    }//GEN-LAST:event_rbSeparatorOtherActionPerformed

    private void rbSeparatorCommaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSeparatorCommaActionPerformed
        // TODO add your handling code here:
        tfOtherSeparator.setEnabled(false);
        tfOtherSeparator.setText("");
    }//GEN-LAST:event_rbSeparatorCommaActionPerformed

    private void rbSeparatorSemicolonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSeparatorSemicolonActionPerformed
        // TODO add your handling code here:
        tfOtherSeparator.setEnabled(false);
        tfOtherSeparator.setText("");
    }//GEN-LAST:event_rbSeparatorSemicolonActionPerformed

    private void rbSeparatorVerticalBarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSeparatorVerticalBarActionPerformed
        // TODO add your handling code here:
        tfOtherSeparator.setEnabled(false);
        tfOtherSeparator.setText("");
    }//GEN-LAST:event_rbSeparatorVerticalBarActionPerformed

    private void btUploadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btUploadMouseClicked

        fieldsOutput = new TreeSet<FieldMedicion>();
        fieldsInput = new ArrayList<FieldMedicion>();
        rulesOutPut = new TreeSet<AbstractRule>();
        frontLine = "";
        nameTemplate = "";

        if (!cbExtType.getSelectedItem().toString().equals("Select Item")) {
            if (rbSeparatorComma.isSelected() == false
                    && rbSeparatorOther.isSelected() == false
                    && rbSeparatorSemicolon.isSelected() == false
                    && rbSeparatorVerticalBar.isSelected() == false
                    && rbSeparatorTab.isSelected() == false) {
                JOptionPane.showMessageDialog(frame, "please, Select one separator.", "Message Selection", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (rbSeparatorOther.isSelected() == true && tfOtherSeparator.getText().equals("")) {
                    JOptionPane.showMessageDialog(frame, "please, Insert separator.", "Message Selection", JOptionPane.ERROR_MESSAGE);
                } else {
                    String separator = "";
                    if (rbSeparatorComma.isSelected()) {
                        separator = ",";
                    }
                    if (rbSeparatorOther.isSelected()) {
                        separator = tfOtherSeparator.getText();
                    }
                    if (rbSeparatorVerticalBar.isSelected()) {
                        separator = "\\|";
                    }
                    if (rbSeparatorSemicolon.isSelected()) {
                        separator = ";";
                    }
                    if (rbSeparatorTab.isSelected()) {
                        separator = "\t";
                    }
                    /**
                     * upload the file and count the number of rows to process
                     */
                    FileDialog Dialog = new FileDialog();
                    String[] readFile = Dialog.Open(cbExtType.getSelectedItem().toString());
                    if (readFile[0].equals("s")) {
                        FileRW fileRW = new FileRW(readFile[1]);
                        fileProcessor = FileProcessorService.getInstance(new File(fileRW.getFile()));
                        try {
                            fieldsInput = fileProcessor.findFieldsByFile(separator, rbHeaders.isSelected(), cbExtType.getSelectedItem().toString());

                            String data[][] = new String[fieldsInput.size()][2];
                            for (int i = 0; i < fieldsInput.size(); i++) {
                                data[i][0] = String.valueOf(fieldsInput.get(i).getValue());
                                data[i][1] = String.valueOf(fieldsInput.get(i).getName());
                            }
                            DefaultTableModel defaultTableModel = new DefaultTableModel(data, columns);
                            this.tbImportColumn.setModel(defaultTableModel);
                            fnAddComboBox();
                            JOptionPane.showMessageDialog(frame, "Columns Size:" + fieldsInput.size(), "Message Information", JOptionPane.INFORMATION_MESSAGE);
                        } catch (IOException ex) {
                            Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } else {
                        JOptionPane.showMessageDialog(frame, "Please, select a file.", "Message upload", JOptionPane.OK_CANCEL_OPTION);
                    }
                }
            }
        } else {
            JOptionPane.showMessageDialog(frame, "please, Select one extension type.", "Error Selection", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btUploadMouseClicked

    private void btSaveTemplateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSaveTemplateMouseClicked
        // TODO add your handling code here:
        String separator = "";
        if (rbSeparatorComma.isSelected()) {
            separator = ",";
        }
        if (rbSeparatorOther.isSelected()) {
            separator = tfOtherSeparator.getText();
        }
        if (rbSeparatorVerticalBar.isSelected()) {
            separator = "\\|";
        }
        if (rbSeparatorSemicolon.isSelected()) {
            separator = ";";
        }
        if (rbSeparatorTab.isSelected()) {
            separator = "\t";
        }

        boolean Error = false;
        if (tfNameTemplante.getText().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert template name.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                nameTemplate = tfNameTemplante.getText().toString();
                ArrayList rdoArray = new ArrayList<AbstractRule>(rulesOutPut);
                fileProcessor.copyFileOnlyTemplateUsingFields(rdoArray, fileContants.getURL_TEMPLATES() + nameTemplate + ".xml", this.tfFrontLine.getText(), this.eltx.getText(), separator);
                cbTemplate.addItem(tfNameTemplante.getText().toString());
            } catch (IOException ex) {
                Error = true;
                Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JAXBException ex) {
                Error = true;
                Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (Error == true) {
                    JOptionPane.showMessageDialog(frame, "Problems saving the template, try again.", "Error File", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(frame, "saving the template", "Message", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btSaveTemplateMouseClicked

    private void btSaveAsConcatenateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSaveAsConcatenateMouseClicked
        // TODO add your handling code here:
        if (tfNameColumnConcatenate.getText().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert column name.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                AbstractRule abstractRule = new AbstractRule();
                abstractRule.setRules(RuleTypesEnum.CONCAT);
                abstractRule.setFieldIni(fieldsInput.get(cbColumn1.getSelectedIndex()));
                if (!tfStart1.getText().isEmpty()) {
                    abstractRule.setStartPositionIni(Integer.parseInt(tfStart1.getText().toString()));
                }
                if (!tfend1.getText().isEmpty()) {
                    abstractRule.setFinalPostionIni(Integer.parseInt(tfend1.getText().toString()));
                }
                abstractRule.setFieldEnd(fieldsInput.get(cbColumn2.getSelectedIndex()));
                if (!tfStart2.getText().isEmpty()) {
                    abstractRule.setStartPositionEnd(Integer.parseInt(tfStart2.getText().toString()));
                }
                if (!tfend2.getText().isEmpty()) {
                    abstractRule.setFinalPostionEnd(Integer.parseInt(tfend2.getText().toString()));
                }
                if (!tfotherConcatenate.getText().isEmpty()) {
                    abstractRule.setSpaceNumber(Integer.parseInt(tfotherConcatenate.getText().toString()));
                }
                abstractRule.setResultName(tfNameColumnConcatenate.getText());
                abstractRule.executeRules();
                rulesOutPut.add(abstractRule);
                FieldMedicion newField = abstractRule.getResultField();
                newField.setName(abstractRule.getResultName());
                fieldsInput.add(newField);
                fnAddComboBox();
                fillRulesTable();
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_btSaveAsConcatenateMouseClicked

    private void btSaveAsDuplicateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSaveAsDuplicateMouseClicked
        // TODO add your handling code here:
        if (tfNameColumDuplicate.getText().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert column name.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else {
            AbstractRule abstractRule = new AbstractRule();
            abstractRule.setRules(RuleTypesEnum.DUPLICITY);
            abstractRule.setFieldIni(fieldsInput.get(cbDuplicate.getSelectedIndex()));
            abstractRule.setResultName(tfNameColumDuplicate.getText().toString());
            abstractRule.executeRules();
            rulesOutPut.add(abstractRule);
            FieldMedicion newField = abstractRule.getResultField();
            newField.setName(abstractRule.getResultName());
            fieldsInput.add(newField);
            fnAddComboBox();
            fillRulesTable();
        }
    }//GEN-LAST:event_btSaveAsDuplicateMouseClicked

    private void bnSaveAsPartitionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bnSaveAsPartitionMouseClicked
        // TODO add your handling code here:
        if (tfNameColumnPartition.getText().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert column name.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else if (tfPartCount.getText().toString().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert part count.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else if (tfLength.getText().toString().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert Length.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else {
            AbstractRule abstractRule = new AbstractRule();
            abstractRule.setRules(RuleTypesEnum.SPLIT);
            abstractRule.setFieldIni(fieldsInput.get(cbPartition.getSelectedIndex()));
            abstractRule.setStartPositionIni(Integer.parseInt(tfPartCount.getText().toString()));
            abstractRule.setFinalPostionIni(Integer.parseInt(tfLength.getText().toString()));
            abstractRule.setResultName(tfNameColumnPartition.getText().toString());
            abstractRule.executeRules();
            rulesOutPut.add(abstractRule);
            FieldMedicion newField = abstractRule.getResultField();
            newField.setName(abstractRule.getResultName());
            fieldsInput.add(newField);
            fnAddComboBox();
            fillRulesTable();
        }
    }//GEN-LAST:event_bnSaveAsPartitionMouseClicked

    private void bnSaveAsFrontlineMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bnSaveAsFrontlineMouseClicked
        // TODO add your handling code here:
        if (tfFrontLine.getText().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert frontline.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else {
            frontLine = tfFrontLine.getText().toString();
        }
    }//GEN-LAST:event_bnSaveAsFrontlineMouseClicked

    private void btAddRightColumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddRightColumnActionPerformed
        // TODO add your handling code here:

        int[] selectedRows = this.tbImportColumn.getSelectedRows();
        for (int i = 0; i < selectedRows.length; i++) {
            AbstractRule abstractRule = new AbstractRule();
            abstractRule.setRules(RuleTypesEnum.ANY);
            abstractRule.setFieldIni(fieldsInput.get(selectedRows[i]));
            abstractRule.setResultName(fieldsInput.get(selectedRows[i]).getName());
            abstractRule.executeRules();
            rulesOutPut.add(abstractRule);
        }
        fillRulesTable();

    }//GEN-LAST:event_btAddRightColumnActionPerformed

    private void btAddleftColumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddleftColumnActionPerformed
        // TODO add your handling code here:    
        List<AbstractRule> fieldTmp = new ArrayList<AbstractRule>(this.rulesOutPut);
        int[] selectedRows = this.tbExportColumn.getSelectedRows();
        for (int i = 0; i < selectedRows.length; i++) {
            AbstractRule abstractRule = new AbstractRule();
            abstractRule.setRules(RuleTypesEnum.ANY);
            abstractRule.setFieldIni(fieldTmp.get(selectedRows[i]).getFieldIni());
            abstractRule.setResultName(fieldTmp.get(selectedRows[i]).getResultField().getName());
            abstractRule.executeRules();
            rulesOutPut.remove(abstractRule);
        }
        fillRulesTable();
    }//GEN-LAST:event_btAddleftColumnActionPerformed

    private void btSaveFileAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveFileAsActionPerformed
        // TODO add your handling code here:

        String separator = "";
        if (tbImportColumn.getRowCount() == 0) {
            JOptionPane.showMessageDialog(frame, "no file loaded", "Error Save", JOptionPane.ERROR_MESSAGE);
        } else if (tbExportColumn.getRowCount() == 0) {
            JOptionPane.showMessageDialog(frame, "no columns to export", "Error Save", JOptionPane.ERROR_MESSAGE);
        } else if (tfSaveAs.getText().toString().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert file Name.", "Error Save", JOptionPane.ERROR_MESSAGE);
        } else if (rbSeparatorComma.isSelected() == false
                && rbSeparatorOther.isSelected() == false
                && rbSeparatorSemicolon.isSelected() == false
                && rbSeparatorVerticalBar.isSelected() == false
                && rbSeparatorTab.isSelected() == false) {
            JOptionPane.showMessageDialog(frame, "please, Select one separator.", "Message Save", JOptionPane.INFORMATION_MESSAGE);
        } else if (rbSeparatorOther.isSelected() == true && tfOtherSeparator.getText().equals("")) {
            JOptionPane.showMessageDialog(frame, "please, Insert separator.", "Message Save", JOptionPane.ERROR_MESSAGE);
        } else if (cbExtType.getSelectedItem().toString().equals("Select Item")) {
        } else {
            boolean Error = false;
            if (rbSeparatorComma.isSelected()) {
                separator = ",";
            }
            if (rbSeparatorOther.isSelected()) {
                separator = tfOtherSeparator.getText();
            }
            if (rbSeparatorVerticalBar.isSelected()) {
                separator = "\\|";
            }
            if (rbSeparatorSemicolon.isSelected()) {
                separator = ";";
            }
            if (rbSeparatorTab.isSelected()) {
                separator = "\t";
            }
            ArrayList rdoArray = new ArrayList<AbstractRule>(rulesOutPut);
            try {
                fileProcessor.copyOnlyFileResultUsingFields(rdoArray, fileContants.getURL_FILES() + tfSaveAs.getText().toString() + "." + cbExtType.getSelectedItem().toString(), separator, frontLine, eltx.getText(), rbHeaders.isSelected());
            } catch (IOException ex) {
                Error = true;
                Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JAXBException ex) {
                Error = true;
                Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (Error == true) {
                    JOptionPane.showMessageDialog(frame, "Problems saving file, try again.", "Error Save", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(frame, "saved file.", "Message Save", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }

    }//GEN-LAST:event_btSaveFileAsActionPerformed

    public void loadSeparatorByString(String separator) {
        rbSeparatorComma.setSelected(false);
        rbSeparatorVerticalBar.setSelected(false);
        rbSeparatorSemicolon.setSelected(false);
        rbSeparatorOther.setSelected(false);
        if (separator.equals(",")) {
            rbSeparatorComma.setSelected(true);
        } else if (separator.equals("\\|")) {
            rbSeparatorVerticalBar.setSelected(true);
        } else if (separator.equals(";")) {
            rbSeparatorSemicolon.setSelected(true);
        } else if (separator.equals("\t")) {
            rbSeparatorTab.setSelected(true);
        } else {
            tfOtherSeparator.setText(separator);
            rbSeparatorOther.setSelected(true);
        }
    }

    private void btApplyTemplateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btApplyTemplateActionPerformed
        // TODO add your handling code here:
        boolean Error = false;
        if (cbTemplate.getSelectedItem() == null || cbTemplate.getSelectedItem().toString().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, upload file.", "Error Template", JOptionPane.ERROR_MESSAGE);
        } else {
            String separator = "";
            if (rbSeparatorComma.isSelected()) {
                separator = ",";
            }
            if (rbSeparatorOther.isSelected()) {
                separator = tfOtherSeparator.getText();
            }
            if (rbSeparatorVerticalBar.isSelected()) {
                separator = "\\|";
            }
            if (rbSeparatorSemicolon.isSelected()) {
                separator = ";";
            }
            if (rbSeparatorTab.isSelected()) {
                separator = "\t";
            }
            try {
                RulesStructure ruleStructureTemplate = null;
                ruleStructureTemplate = fileProcessor.findFieldsByFileWithTemplate(separator, rbHeaders.isSelected(), "varchar", fileContants.getURL_TEMPLATES() + cbTemplate.getSelectedItem().toString());
                List<AbstractRule> listAbstractRules = ruleStructureTemplate.getRules();
                eltx.setText(ruleStructureTemplate.getEndLine());
                tfFrontLine.setText(ruleStructureTemplate.getFrontLine());
                loadSeparatorByString(ruleStructureTemplate.getSeparator());
                rulesOutPut = new TreeSet<AbstractRule>(listAbstractRules);

                fillRulesTable();
            } catch (IOException ex) {
                Error = true;
                Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JAXBException ex) {
                Error = true;
                Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (Error == true) {
                    JOptionPane.showMessageDialog(frame, "Problems apply template, try again.", "Error Type", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(frame, "Template was applied.", "Message", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btApplyTemplateActionPerformed

    private void bnDeleteTemplateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDeleteTemplateActionPerformed
        // TODO add your handling code here:
        try {
            fileProcessor.deleteTemplate(fileContants.getURL_TEMPLATES() + cbTemplate.getSelectedItem().toString());
            cbTemplate.removeItem(cbTemplate.getSelectedItem());
        } catch (Exception e) {
            Logger.getLogger(JFTemplateGenerator.class.getName()).log(Level.SEVERE, null, e);
            JOptionPane.showMessageDialog(frame, "Problems delete the template, try again.", "Error Type", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_bnDeleteTemplateActionPerformed

    private void btSaveAsConcatenateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveAsConcatenateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btSaveAsConcatenateActionPerformed

    private void btUpRuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpRuleActionPerformed
        // TODO add your handling code here
        int indexToUp = tbExportColumn.getSelectedRow();
        ArrayList<AbstractRule> abstractRules = new ArrayList<AbstractRule>(rulesOutPut);
        if ((indexToUp - 1) >= 0) {
            AbstractRule abstractRuleIni = abstractRules.get(indexToUp);
            AbstractRule abstractRuleEnd = abstractRules.get(indexToUp - 1);

            abstractRules.set(indexToUp - 1, abstractRuleIni);
            abstractRules.set(indexToUp, abstractRuleEnd);
        }
        rulesOutPut = new TreeSet<AbstractRule>(abstractRules);
        fillRulesTable();
    }//GEN-LAST:event_btUpRuleActionPerformed

    private void bnDownRuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDownRuleActionPerformed
        // TODO add your handling code here:
        int indexToUp = tbExportColumn.getSelectedRow();
        ArrayList<AbstractRule> abstractRules = new ArrayList<AbstractRule>(rulesOutPut);
        if ((indexToUp + 1) < abstractRules.size()) {
            AbstractRule abstractRuleIni = abstractRules.get(indexToUp);
            AbstractRule abstractRuleEnd = abstractRules.get(indexToUp + 1);

            abstractRules.set(indexToUp + 1, abstractRuleIni);
            abstractRules.set(indexToUp, abstractRuleEnd);
        }
        rulesOutPut = new TreeSet<AbstractRule>(abstractRules);
        fillRulesTable();
    }//GEN-LAST:event_bnDownRuleActionPerformed

    private void btClearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btClearMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_btClearMouseClicked

    private void btClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btClearActionPerformed
        jpManager.removeAll();
        this.initComponents();


        fieldsOutput = new TreeSet<FieldMedicion>();
        fieldsInput = new ArrayList<FieldMedicion>();
        rulesOutPut = new TreeSet<AbstractRule>();
        frontLine = "";
        nameTemplate = "";

        this.setVisible(false);
        JFTemplateGenerator jfTemplateGenerator = new JFTemplateGenerator();
        if (this.getFileContants().getURL_FILES() != null) {
            jfTemplateGenerator.setFileContants(this.getFileContants());
            jfTemplateGenerator.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "the folder must be configured", "Error Type", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btClearActionPerformed

    private void btUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUploadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btUploadActionPerformed

    private void btAddColumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddColumnActionPerformed
        // TODO add your handling code here:
        if (tbImportColumn.getRowCount() == 0) {
            JOptionPane.showMessageDialog(frame, "no file loaded", "Error Save", JOptionPane.ERROR_MESSAGE);
        } else if (tfNameAddColumn.getText().isEmpty()) {
            JOptionPane.showMessageDialog(frame, "please, Insert Column Name.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else if (!rbAddColumnNull.isSelected() && !rbAddColumnZero.isSelected()) {
            JOptionPane.showMessageDialog(frame, "please, Select default value.", "Error Type", JOptionPane.ERROR_MESSAGE);
        } else {

            List<String> values = new ArrayList<String>();
            if (fieldsInput.get(0) != null) {

                int valueNumber = fieldsInput.get(0).getValues().size();
                for (int i = 0; i < valueNumber; i++) {
                    if (rbAddColumnZero.isSelected()) {
                        values.add("0");
                    } else {
                        if (rbAddColumnNull.isSelected()) {
                            values.add("");
                        }
                    }
                }
            }
            AbstractRule abstractRule = new AbstractRule();
            abstractRule.setRules(RuleTypesEnum.ANY);
            FieldMedicion fieldMedicionIni = new FieldMedicion();
            fieldMedicionIni.setValues(values);
            abstractRule.setFieldIni(fieldMedicionIni);
            abstractRule.setResultName(tfNameAddColumn.getText());
            abstractRule.executeRules();
            abstractRule.getResultField().setName(abstractRule.getResultName());
            rulesOutPut.add(abstractRule);

            fillRulesTable();
        }
    }//GEN-LAST:event_btAddColumnActionPerformed

    private void btSaveTemplateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveTemplateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btSaveTemplateActionPerformed

    private void bnSaveAsPartitionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnSaveAsPartitionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bnSaveAsPartitionActionPerformed

    private void btSaveAsDuplicateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveAsDuplicateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btSaveAsDuplicateActionPerformed

    private void btSaveFileAsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSaveFileAsMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_btSaveFileAsMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFTemplateGenerator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgSeparatorColumn;
    private javax.swing.ButtonGroup bgTypes;
    private javax.swing.ButtonGroup bgValuesDefault;
    private javax.swing.JButton bnDeleteTemplate;
    private javax.swing.JButton bnDownRule;
    private javax.swing.JButton bnSaveAsFrontline;
    private javax.swing.JButton bnSaveAsPartition;
    private javax.swing.JButton btAddColumn;
    private javax.swing.JButton btAddRightColumn;
    private javax.swing.JButton btAddleftColumn;
    private javax.swing.JButton btApplyTemplate;
    private javax.swing.JButton btClear;
    private javax.swing.JButton btSaveAsConcatenate;
    private javax.swing.JButton btSaveAsDuplicate;
    private javax.swing.JButton btSaveFileAs;
    private javax.swing.JButton btSaveTemplate;
    private javax.swing.JButton btUpRule;
    private javax.swing.JButton btUpload;
    private javax.swing.JComboBox cbColumn1;
    private javax.swing.JComboBox cbColumn2;
    private javax.swing.JComboBox cbDuplicate;
    private javax.swing.JComboBox cbExtType;
    private javax.swing.JComboBox cbPartition;
    private javax.swing.JComboBox cbTemplate;
    private javax.swing.JTextField eltx;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel jpConcatenate;
    private javax.swing.JPanel jpDuplicate;
    private javax.swing.JPanel jpManager;
    private javax.swing.JPanel jpPartition;
    private javax.swing.JPanel jpProperties;
    private javax.swing.JPanel jpTemplate;
    private javax.swing.JRadioButton rbAddColumnNull;
    private javax.swing.JRadioButton rbAddColumnZero;
    private javax.swing.JRadioButton rbHeaders;
    private javax.swing.JRadioButton rbSeparatorComma;
    private javax.swing.JRadioButton rbSeparatorOther;
    private javax.swing.JRadioButton rbSeparatorSemicolon;
    private javax.swing.JRadioButton rbSeparatorTab;
    private javax.swing.JRadioButton rbSeparatorVerticalBar;
    private javax.swing.JScrollPane spExportColumns;
    private javax.swing.JScrollPane spImportColumn;
    private javax.swing.JTable tbExportColumn;
    private javax.swing.JTable tbImportColumn;
    private javax.swing.JTextField tfFrontLine;
    private javax.swing.JTextField tfLength;
    private javax.swing.JTextField tfNameAddColumn;
    private javax.swing.JTextField tfNameColumDuplicate;
    private javax.swing.JTextField tfNameColumnConcatenate;
    private javax.swing.JTextField tfNameColumnPartition;
    private javax.swing.JTextField tfNameTemplante;
    private javax.swing.JTextField tfOtherSeparator;
    private javax.swing.JTextField tfPartCount;
    private javax.swing.JTextField tfSaveAs;
    private javax.swing.JTextField tfStart1;
    private javax.swing.JTextField tfStart2;
    private javax.swing.JTextField tfend1;
    private javax.swing.JTextField tfend2;
    private javax.swing.JTextField tfotherConcatenate;
    // End of variables declaration//GEN-END:variables

    private void fillRulesTable() {
        List<AbstractRule> fieldTmp = new ArrayList<AbstractRule>(this.rulesOutPut);
        String data[][] = new String[fieldTmp.size()][2];
        for (int i = 0; i < fieldTmp.size(); i++) {
            data[i][0] = String.valueOf(i);
            data[i][1] = String.valueOf(fieldTmp.get(i).getResultField().getName());
        }
        DefaultTableModel defaultTableModel = new DefaultTableModel(data, columns);
        this.tbExportColumn.setModel(defaultTableModel);
    }
}
